from sqlalchemy import (
    Column, Integer, String, Enum, TIMESTAMP, ForeignKey, Text, JSON, BigInteger, SmallInteger
)
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    username = Column(String(50), unique=True, nullable=False)
    email = Column(String(100), unique=True, nullable=False)
    password_hash = Column(String(255), nullable=False)
    salt = Column(String(64), nullable=False)
    user_type = Column(Enum('student', 'teacher', 'staff', 'admin'), default='staff', nullable=False)
    department = Column(String(50), default='general')
    status = Column(Enum('active', 'disabled'), default='active')
    created_at = Column(TIMESTAMP)

class UserAttribute(Base):
    __tablename__ = 'user_attributes'
    user_id = Column(Integer, ForeignKey('users.id', ondelete='CASCADE'), primary_key=True)
    attr_key = Column(String(50), primary_key=True)
    attr_value = Column(String(255))

class Role(Base):
    __tablename__ = 'roles'
    id = Column(Integer, primary_key=True)
    role_name = Column(String(50), unique=True, nullable=False)
    description = Column(String(255))

class UserRole(Base):
    __tablename__ = 'user_roles'
    user_id = Column(Integer, ForeignKey('users.id', ondelete='CASCADE'), primary_key=True)
    role_id = Column(Integer, ForeignKey('roles.id', ondelete='CASCADE'), primary_key=True)

class Permission(Base):
    __tablename__ = 'permissions'
    id = Column(Integer, primary_key=True)
    resource = Column(String(50), nullable=False)
    action = Column(String(50), nullable=False)
    attribute_condition = Column(JSON)
    __table_args__ = (
        {'mysql_engine': 'InnoDB'},
    )

class RolePermission(Base):
    __tablename__ = 'role_permissions'
    role_id = Column(Integer, ForeignKey('roles.id', ondelete='CASCADE'), primary_key=True)
    permission_id = Column(Integer, ForeignKey('permissions.id', ondelete='CASCADE'), primary_key=True)

class OperationLog(Base):
    __tablename__ = 'operation_logs'
    id = Column(BigInteger, primary_key=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey('users.id', ondelete='SET NULL'), nullable=True)
    session_id = Column(String(100))
    operation_type = Column(String(50), nullable=False)
    operation_target = Column(String(100))
    operation_time = Column(TIMESTAMP)
    operation_details = Column(Text)
    ip_address = Column(String(45))
    user_agent = Column(String(255))
    status = Column(Enum('success', 'fail'), default='success')

class LogOperationType(Base):
    __tablename__ = 'log_operation_types'
    id = Column(SmallInteger, primary_key=True, autoincrement=True)
    type_code = Column(String(50), unique=True, nullable=False)
    description = Column(String(100), nullable=False)

class LogDetail(Base):
    __tablename__ = 'log_details'
    log_id = Column(BigInteger, ForeignKey('operation_logs.id', ondelete='CASCADE'), primary_key=True)
    detail_key = Column(String(50), primary_key=True)
    detail_value = Column(Text) 